<template>
    <van-nav-bar title="确认订单" left-text="" left-arrow @click-left="goBack" />
    <!-- 收货地址 -->
    <van-contact-card type="add" @click="toAddressPage" v-if="JSON.stringify(address) == '{}'" />
    <van-contact-card type="edit" :tel="address.mobile" :name="address.consignee" @click="toAddressPage" v-else />

    <!-- 商品信息 -->
    <van-card :price="price" :desc="'库存：' + desc" :title="title" :thumb="thumb" :num="num">
        <template #tags>
            <div>单价： {{ price }}</div>
        </template>

    </van-card>

    <!-- 订单备注 -->
    <van-cell-group inset>
        <van-field v-model="remark" rows="2" autosize label="订单备注" type="textarea" maxlength="50" placeholder="请输入订单备注"
            show-word-limit />
    </van-cell-group>
    <!-- 订单金额 -->
    <van-submit-bar :price="price" button-text="提交订单" @submit="onSubmit" />
</template>
<script setup>
const goBack = () => {
    history.back();
};
const router = useRouter();
const route = useRoute();
const { cookies } = useCookies();
const LoginUser = reactive(cookies.get("LoginUser") ?? {});
const address = ref(cookies.get("Address") ?? {});
const cartids = ref(route.query.cartids ?? 0);
const proid = ref(route.query.proid ?? 0);
const cartlist = ref([]);
const num = ref(0);
const price = ref(0);
const title = ref("");
const desc = ref(0);
const thumb = ref('');
const remark = ref("");
const toAddressPage = () => {
    router.push("/business/address/index?action=order");
};
onBeforeMount(() => {
    getCartData();
    if (JSON.stringify(address.value) == "{}") {
        getAddressData();
    }
});
const getAddressData = async () => {
    let result = await Api.addressOder({
        busid: LoginUser.id,
    });
    if (result.code == 1) {
        address.value = result.data;
    }
};
const getCartData = async () => {
    let result = await Api.singleIndex({
        busid: LoginUser.id,
        proid: proid.value
    });
    if (result.code == 1) {
        cartlist.value = result.data;
        console.log(cartlist.value);
        price.value = cartlist.value.price;
        num.value = cartlist.value.num;
        desc.value = cartlist.value.desc;
        thumb.value = cartlist.value.thumb_text;
        title.value = cartlist.value.name;
    }
};



const onSubmit = async () => {
    showConfirmDialog({
        message: "选择支付类型",
    }).then(() => {
    })
        .then(() => {

        })
        .catch(async () => {
            let data = {
                busid: LoginUser.id,
                proid: proid.value,
                addressid: address.value.id,
                remark: remark.value,
            }
            let result = await Api.singleAdd(data);
            if (result.code == 1) {
                router.push("/business/order/index");
            }else{
                showFailToast(result.msg)
            }
        });
};
</script>